Ext.regModel('HostInfo', {
    fields: [{
        name: "isActive",
        type: "boolean",
        mapping: "fields.isActive"
    },{
        name: "size",
        type: "int",
        mapping: "fields.size"
    }]
});

Ext.regModel('HostsIPs', {
    fields: [{
        name: "ip",
        type: "string",
        mapping: "fields.ip"
    }]
});

Ext.regModel('Hosts', {
    fields: [{
        name: "name",
        type: "string",
        mapping: "fields.fullHostname"
    },{
        name: "size",
        type: "int",
        mapping: "fields.size"
    }]
});


Ext.define('Ext.supreme.HostInfoPanel', {
    extend: 'Ext.Panel',
    alias: ['widget.hostinfo'],


    host: undefined,
    host_is_active: undefined,
    hostname: undefined,
    ip_addr: undefined,
    
    initComponent : function(){
        var ips_store = Ext.create('Ext.data.Store', {
            model: 'HostsIPs',
            proxy: {
                type: 'ajax',
                url : '/ajax/host/' + this.host + "/ip",
                reader: {
                    type: 'json'
                }
            },
            autoLoad: true
        });

        var host_data = Ext.create('Ext.data.Store', {
            model: 'HostInfo',
            proxy: {
                type: 'ajax',
                url : '/ajax/host/' + this.host,
                reader: {
                    type: 'json'
                }
            },
            autoLoad: true,
            listeners:{
                load:function(store, records, options) {
                    host_is_active.setValue(host_data.getAt(0).data.isActive);
                    data_size.setValue(Ext.supreme.filesizeConverter(host_data.getAt(0).data.size, true));
                }
            }
        });

        listOfIp = new Ext.list.ListView({
            fieldLabel: 'Adresy',
            store: ips_store,
            multiSelect: true,
            hideHeaders: true,
            emptyText: 'Brak',
            readOnly: true,
            anchor: '100%',
            reserveScrollOffset: true,
            columns: [{
                header: 'IP',
                dataIndex: 'ip'
            }]
        });

        hostname = new Ext.form.TextField({
            fieldLabel: 'Nazwa',
            name: 'name',
            readOnly: true,
            value: this.host,
            border: false,
            anchor: '100%',
            id: 'host_info_name'
        });
        
        data_size = new Ext.form.TextField({
            fieldLabel: 'Rozmiar',
            name: 'size',
            readOnly: true,
            border: false,
            anchor: '100%',
            id: 'host_info_size'
        });
                
        host_is_active = new Ext.form.Checkbox({
            fieldLabel: 'Aktywny',
            name: 'active',
            readOnly: true,
            border: false,
            anchor: '100%',
            id: 'host_info_active'
        });


        var defConfig = {
            host: undefined,
            items:[
            {
                columnWidth: 1,
                xtype: 'fieldset',
                labelWidth: 90,
                defaults: {
                    width: 140,
                    border:false
                },    // Default config options for child items
                defaultType: 'textfield',
                autoHeight: true,
                bodyStyle: Ext.isIE ? 'padding:0 0 5px 15px;' : 'padding:10px 15px;',
                border: false,
                style: {
                    "margin-left": "10px", // when you add custom margin in IE 6...
                    "margin-right": Ext.isIE6 ? (Ext.isStrict ? "-10px" : "-13px") : "0"  // you have to adjust for it somewhere else
                },
                items: [hostname,listOfIp,data_size,host_is_active]

            }]
        };

        Ext.applyIf(this,defConfig);

        this.callParent();

    },
    
    afterRender : function(){

        var wh = this.ownerCt.getSize();
        Ext.applyIf(this, wh);

        Ext.supreme.HostInfoPanel.superclass.afterRender.call(this);
    }

});

Ext.define('Ext.supreme.HostInfoWindow', {
    extend: 'Ext.Window',

    host: undefined,
    titlePrefix: 'Host info ',
    fullTitle: undefined,

    constructor : function(){
        var me = this;
        me.title = me.fullTitle || me.titlePrefix + me.host;
        me.width = 400;
        me.height = 400;
        me.layout = 'fit';
        me.items = [{
            xtype: 'hostinfo',
            host: this.host
        }];
        return this;
    }
});

Ext.define('Ext.supreme.HostsWindow', {
    extend: 'Ext.Window',
    host: undefined,
    title: 'Hosty',
    width: 400,
    height: 400,
    layout: 'fit',
    items: undefined,

    initComponent : function(){
        var me = this;
        var all_hosts_store = Ext.create('Ext.data.Store', {
            model: 'Hosts',
            proxy: {
                type: 'ajax',
                url : '/ajax/host/all',
                reader: {
                    type: 'json'
                }
            }
        });

        all_hosts_store.load();

        var grid = Ext.create('Ext.grid.GridPanel', {
            store: all_hosts_store,
            columnLines: true,
            loadMask: {
                msg: "\u0141adowanie",
                store: all_hosts_store
            },
            headers: [
            {
                text: "Nazwa",
                dataIndex: 'name',
                width: 100,
                sortable: true
            },{
                text: "Udost\u0119pnione",
                dataIndex: 'size',
                sortable: true,
                renderer: Ext.supreme.filesizeConverter
            }],
            viewConfig: {
                forceFit: true
            }
        });
        
        Ext.applyIf(this,{
            items: [grid]
        });

        //Ext.supreme.HostInfoWindow.superclass.initComponent.call(this);
        this.callParent()
    }
});

